package com.gewu.advertisement.core.common

import android.util.Log
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.CoroutineContext

/**
 * @author htt 2020/8/25
 */
@Suppress("FunctionName")
internal fun LogExceptionHandler(tag: String) = CoroutineExceptionHandler { _, throwable ->
    Log.e(tag, "coroutine error", throwable)
}

/**
 * 通过launch创建的任务发生异常时打印日志 不会影响到其他Job
 */
@Suppress("FunctionName")
internal fun SupervisorLogScope(
    tag: String,
    context: CoroutineContext = Dispatchers.Default
): CoroutineScope =
    CoroutineScope(context + SupervisorJob() + LogExceptionHandler(tag))

@Suppress("FunctionName")
internal inline fun <reified T> SupervisorLogScope(context: CoroutineContext = Dispatchers.Default): CoroutineScope = SupervisorLogScope(T::class.java.simpleName,context)



